package br.edu.ufc.deti.broker.persistencia;

import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import java.sql.*;
import java.util.List;

import javax.crypto.BadPaddingException;
import javax.crypto.IllegalBlockSizeException;
import javax.crypto.NoSuchPaddingException;

import br.edu.ufc.deti.beans.Autenticacao;
import br.edu.ufc.deti.beans.Recurso;
import br.edu.ufc.deti.util.Crypto;

public class PrsAutenticacao {

	private static PrsAutenticacao instance;
	
	public static synchronized PrsAutenticacao getInstance(){
		if( instance == null )
			instance = new PrsAutenticacao();
		return instance;
	}
	
	/**
	 * @param stm
	 * @param usuario
	 * @param senha
	 * @return
	 */
	public void autenticaUsuario( Statement stm, Autenticacao autenticacao ) {
		ResultSet rs;
		String usuario = autenticacao.getUsuario();
		String senha = null;
		try {
			//senha = Crypto.decrypt(autenticacao.getSenha());
			senha = autenticacao.getSenha();
			String query = "SELECT id FROM usuario where nome = '"+usuario+"' and senha = '"+senha+"'"; 
			rs = stm.executeQuery(query);
			while (rs.next()) {
				autenticacao.setID(rs.getInt("id"));
			}
			rs.close();
/*		} catch (InvalidKeyException e1) {
			e1.printStackTrace();
		} catch (NoSuchAlgorithmException e1) {
			e1.printStackTrace();
		} catch (NoSuchPaddingException e1) {
			e1.printStackTrace();
*/		} catch (IllegalStateException e1) {
			e1.printStackTrace();
/*		} catch (IllegalBlockSizeException e1) {
			e1.printStackTrace();
		} catch (BadPaddingException e1) {
			e1.printStackTrace();
*/		} catch (SQLException e1) {
			e1.printStackTrace();
		}
	}
}
